package com.vikutorika.dao.imp;

import com.vikutorika.dao.GroupUserDAO;
import com.vikutorika.dao.BasicDAO;
import com.vikutorika.entity.GroupTextMessage;
import com.vikutorika.entity.GroupUser;
import com.vikutorika.util.V_DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GroupUserDAOImp extends BasicDAO<GroupUser> implements GroupUserDAO {
    @Override
    public List<GroupUser> getAll() {
        Connection conn= null;
        String sql = "select * from group_text_message ;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            return queryRunner.query(conn,sql, new BeanListHandler<>(GroupUser.class));
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
    }

    @Override
    public int totalRow() {
        return 0;
    }

    @Override
    public List<GroupUser> getByGID(Integer gid) {
        Connection conn= null;
        String sql = "select * from group_user where gid = ?;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            return queryRunner.query(conn,sql, new BeanListHandler<>(GroupUser.class),gid);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
    }

    @Override
    public List<GroupUser> getByUID(Integer uid) {
        Connection conn= null;
        String sql = "select * from group_user where uid = ?;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            return queryRunner.query(conn,sql, new BeanListHandler<>(GroupUser.class),uid);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
    }


    @Override
    public GroupUser getByGID_AND_UID(Integer gid, Integer uid) {
        Connection conn= null;
        String sql = "select * from group_user where uid = ? and gid = ?;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            return queryRunner.query(conn,sql, new BeanHandler<>(GroupUser.class),uid,gid);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
    }

    @Override
    public int update(GroupUser user) {
        Connection conn= null;
        String sql = "update group_user set pull_time = ? where uid = ? and gid =? ;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            return queryRunner.update(conn,sql,user.getPull_time(), user.getUid(),user.getGid());
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
    }
}
